package pwr.online.cow;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import pwr.online.cow.model.BoardUpdateListener;
import pwr.online.cow.model.Position;

@SuppressWarnings("serial")
public class BoardContoller extends JPanel implements BoardUpdateListener, ChangeListener, ActionListener {

	private JSlider historySlider;
	private LinkedList<BoardControllerListener> mListeners;
	
	public BoardContoller() {
		
		mListeners = new LinkedList<BoardControllerListener>();

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
 
        JLabel sliderLabel = new JLabel("Cow search step", JLabel.CENTER);
        sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
 
        historySlider = new JSlider(0, 0); 

        historySlider.setMajorTickSpacing(10);
        historySlider.setMinorTickSpacing(1);
        historySlider.setPaintLabels(true);
        historySlider.addChangeListener(this);
        
        JButton recompute = new JButton("Recompute");
        recompute.setAlignmentX(Component.CENTER_ALIGNMENT);
        recompute.addActionListener(this);
        
        JComboBox combo = createComboBox();
        combo.addActionListener(this);
        
        add(sliderLabel);
        add(historySlider);
        add(combo);
        add(recompute);
        setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	}
	
	public JComboBox createComboBox() {
        JComboBox combo = new JComboBox(new String[]{"RIGHT", "UP", "LEFT", "DOWN", "RANDOM"});
        
        return combo;
	}

	@Override
	public void OnBoardHistoryAdded(Position addedPosition) {
		historySlider.setMaximum(historySlider.getMaximum()+1);
		historySlider.setValue(historySlider.getMaximum());
	}

	@Override
	public void OnBoardHistoryCleared() {
		historySlider.setMaximum(1);
	}

	public void addListener(BoardControllerListener listener) {
		mListeners.add(listener);
	}
	
	@Override
	public void stateChanged(ChangeEvent arg0) {
		JSlider slider = (JSlider) arg0.getSource();
		for (BoardControllerListener listener : mListeners) {
			listener.HistoryViewChanged(slider.getValue());
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() instanceof JButton) { 
			for (BoardControllerListener l : mListeners) {
				l.RestartAlgorithm();
			}
		} else if (arg0.getSource() instanceof JComboBox) {
			JComboBox combo = (JComboBox) arg0.getSource();
			String direction = (String)combo.getSelectedItem();
			int nx = 1, ny = 0;
			if (direction.equals("RIGHT")) {
				nx = 1;
				ny = 0;
			} else if (direction.equals("UP")) {
				nx = 0;
				ny = -1;
			} else if (direction.equals("LEFT")) {
				nx = -1;
				ny = 0;
			} else if (direction.equals("DOWN")) {
				nx = 0;
				ny = 1;
			} else if (direction.equals("RANDOM")) {
				Random r = new Random();
				do {
					nx = r.nextInt(3) - 1;
					ny = r.nextInt(3) - 1;
				} while (Math.abs(nx + ny) != 1);
			}
			for (BoardControllerListener l : mListeners) {
				l.NewStartDirection(nx, ny);
			}
		}
	}
	
	
}
